package springboot.Tools.Date;

import springboot.Tools.time;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class StartAndEndTime {
	
	
	public static Week getWeek(int monthDiff, int weekDiff) {
		return getFullWeek(monthDiff, weekDiff);
	}
	
	public static Day getDay(int monthDiff, int dayDiff) {
		Calendar calendar = Calendar.getInstance();
		// 设置开始时间
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		calendar.add(Calendar.MONTH, monthDiff);
		calendar.add(Calendar.DAY_OF_MONTH, dayDiff);
		Day day = new Day();
		day.setDayStartTime(calendar.getTime());
		calendar.add(Calendar.SECOND, 24 * 3600 - 1);
		day.setDayEndTime(calendar.getTime());
		day.setDayString(time.dateFormat(day.getDayStartTime()));
		return day;
	}
	
	public static Day getDay(int dayDiff) {
		return getDay(0, dayDiff);
	}
	
	public static List<String> getDaysString(int dayNum, int diff) {
		List<String> strings = new ArrayList<>();
		for (int i = 0; i < dayNum; i++) {
			Day day = getDay(diff-i);
			strings.add(i, day.getDayString());
		}
		return strings;
	}
	
	
	public static List<Integer> test(int monthDiff, int weekDiff) {
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		
		// 设置开始时间
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.getActualMinimum(Calendar.DAY_OF_MONTH), 0, 0, 0);
		calendar.add(Calendar.MONTH, monthDiff);
		
		if (weekDiff <= 0 || weekDiff > calendar.getActualMaximum(Calendar.WEEK_OF_MONTH)) {
//            如果周位置小于等于零，或者大于最大周数返回null
			return null;
		}
		List<Integer> list = new ArrayList<>();
		for (int i = 0; i < calendar.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
			calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), i + 1);
			list.add(calendar.get(Calendar.WEEK_OF_MONTH));
		}
		return list;
	}
	
	public static FullWeek getFullWeek(int monthDiff, int weekDiff) {
		Calendar calendar = Calendar.getInstance();
		// 设置开始时间
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.getActualMinimum(Calendar.DAY_OF_MONTH), 0, 0, 0);
		List<Integer> list = test(monthDiff, weekDiff);
		
		FullWeek week = new FullWeek();
		week.setWeekIndex(weekDiff);
//        设置日期的开始为数组中第一次出现周标志位的index，加一天，Calendar的天数从零开始
		assert list != null;
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				list.indexOf(weekDiff) + 1);
		week.setWeekStartTime(calendar.getTime());
//        设置日期的结束为数组中最后一次出现周标志位的index，加两天，获取后一天的开始时间，同时是本周的结束时间
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				list.lastIndexOf(weekDiff) + 2);
		week.setWeekEndTime(new Date(calendar.getTime().getTime() - 1000));
		return week;
	}
	
	public static Month getMonth(int monthDiff) {
		return getFullMonth(monthDiff);
	}
	
	public static FullMonth getFullMonth(int monthDiff) {
		FullMonth month = new FullMonth();
		Calendar calendar = Calendar.getInstance();
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				1, 0, 0, 0);
		calendar.add(Calendar.MONTH, monthDiff);
		month.setMonthStartTime(calendar.getTime());
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.getActualMaximum(Calendar.DAY_OF_MONTH), 23, 59, 59);
		
		month.setMonthEndTime(calendar.getTime());
		month.setMonthString(time.dateFormat(calendar.getTime(), 3));
		month.setWeekCnt(calendar.getActualMaximum(Calendar.WEEK_OF_MONTH));
		return month;
	}
	
	public static int getWeekCount(int monthDiff) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, monthDiff);
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		return calendar.getActualMaximum(Calendar.WEEK_OF_MONTH);
	}
	
}
